#-*-coding:utf-8-*-
#
#修改文件名
#规则：当前日期 时间 序号
#示例：2020-01-01 193015 ##.suffix
#
#1.遍历指定文件夹下文件
#2.截取当前文件扩展名
#3.当前日期+时间+序号+”.“后缀
#


#文件夹
#DIR_PATH = 'C:/E/work_myself/python_study/get_pic_from_weixin/testRename'
DIR_PATH = 'C:/E/temp/pic/'

#当前日期、时刻
import time,sys
file_name = time.strftime('%Y-%m-%d %H%M%S',time.localtime())


#修改jfif的扩展名为jpeg
suffix_map = {'.jfif':'.jpeg'}
def changeSuffix(suffix):
    if suffix_map.__contains__(suffix):
        return suffix_map[suffix]
    return suffix


import os
#遍历文件、重命名
def peerFiles(dirPath):
    file_index = 1
    for f in os.listdir(dirPath):
        print(f)
        dot_index = str(f).rfind('.')
        #print(f.index('.'))
        print(f[dot_index:])
        file_suffix = f[dot_index:]
        if file_suffix not in ['.gif','.jpg','.jpeg','.png','.jfif','.webp']:
            continue
        os.rename(dirPath+'/'+f,dirPath+'/'+file_name+' '+str(file_index)+changeSuffix(file_suffix))
        file_index+=1

#遍历文件、按数字顺序重命名 方便执行cmd命令 
#在命令行里执行renamefiles.py 0
def peerFilesForNo(dirPath):
    file_index = 1
    for f in os.listdir(dirPath):
        print(f)
        dot_index = str(f).index('.')
        #print(f.index('.'))
        print(f[dot_index:])
        file_suffix = f[dot_index:]
        os.rename(dirPath+'/'+f,dirPath+'/'+str(file_index)+changeSuffix(file_suffix))
        file_index+=1

import cn2an,re
def transCnNum2AnNum(dirPath):
    cn_pattern = '第([一二三四五六七八九十零]*)回'
    recn = re.compile(cn_pattern)
    file_index = 1
    for f in os.listdir(dirPath):
        targetnum = recn.findall(f)
        annum = 0
        if len(targetnum)>0:
            cnnum = targetnum[0]
            annum = '{:0>2d}'.format(cn2an.cn2an(cnnum))
            newname = f.replace(cnnum,str(annum)+cnnum)
            os.rename(dirPath+'/'+f,dirPath+'/'+newname)
            print(newname)



if __name__ == '__main__':
    if len(sys.argv)>1 :
        peerFilesForNo(DIR_PATH)
    else :
        peerFiles(DIR_PATH)